
function file_sort(val){
	yanhx.exec("fileHandle.sort",val);
}
function file_fill(ret){
	yanhx.exec("fileHandle.fillTable",ret);
}
function file_scroll(){
	yanhx.exec("fileHandle.scrollQry");
}
yanhx.addCommon("load",function(){
	//head 需定义参数param 普通查询包含 sort head template 
	var head={
	title:{css:{width:"100%"}},
	create_date:{css:{width:"15%"}},
	file_size:{css:{width:"15%"}},
	fen_shu:{css:{width:"15%"},bindFunc:"bindFenshu"},
	ji_fen:{css:{width:"15%"}},
	name:{css:{width:"20%"}},
	content:{css:{width:"100%"}},
	ctrl:{css:{width:"20%"},bindFunc:"fileCtrl"}
};
tableHandle.addFunc("bindFenshu",function(key){
	var val=this[key];
	var c="red";
	if(val>=80){
		c="rgb(14, 156, 42)";
	}
	else if(val>=60){
		c="rgb(241, 141, 14)";
	}
	return "<span style='color:"+c+"'>"+val/10.0+"分</span>";
});
tableHandle.addFunc("fileCtrl",function(key){
	return "<button class='btn' callFunc='click:fileHandle.view'>查看评论</button><button  callFunc='click:fileHandle.download' class='btn'>下载</button>";
});
var template="<div class='table_template'>" +
				"<div class='tr'>" +
					"<div class='td title' jtext='title'></div>" +
				"</div>" +
				"<div class='tr'>" +
					"<div class='td gray' jtext='content'></div>" +
				"</div>" +
				"<div class='tr normal'>" +
					"<div class='td' jtext='name'></div>" +
					"<div class='td' jtext='create_date'></div>" +
					"<div class='td' jtext='file_size'></div>" +
					"<div class='td' jtext='fen_shu'></div>" +
					"<div class='td money' jtext='ji_fen'></div>" +
					"<div class='td' jtext='ctrl'></div>" +
				"</div>" +
			"</div>";

var sort=[{id:"a.fen_shu",name:"评分排行"},
          {id:"a.ji_fen",name:"所需积分"}];

var param={
	sort:sort,
	head:head,
	temp:template
};

	yanhx.exec("fileHandle.init",param);
	yanhx.exec("fileHandle.qry");
	yanhx.exec("scrollHandle.add",file_scroll)
});
yanhx.addComponent("fileHandle",{
	bean:null,
	clean:false,
	canPage:true,
	init:function(param){
		this.tableDiv=$("#file_table");
		this.table=this.tableDiv.children("#tableBody");
		this.table.listTable({
			//头配置
			curCfg:param.head,
			//模板
			curTemp:$(param.temp)
		});	
		if(param.sort){
			var t=$("#file_sort");
			var sel=t.find("select");
			$(param.sort).each(function(){
				$("<option value="+this.id+">"+this.name+"</option>").appendTo(sel);
			});
		}
	},
	qry:function(){
		var bean=$("#file").getBean();		
		this.bean=bean;
		this.bean.page=1;
		this.bean.pageSize=15;
		this.bean.qryTotal="y";
		this.canPage=true;
		this.clean=true;
		this.doQuery();	
	},
	doQuery:function(){
		$.java({
			action:'file.qryFile',
			param:this.bean,
			showload:true,
			callback:file_fill
		});
	},
	add:function(){
		$(common.file).modelWin({
			title:'上传文件',
			valid:'file_valid'
		});
	},
	sort:function(val){
		this.clean=true;
		this.bean.qryTotal=null;
		this.bean.page=1;
		this.canPage=true;
		this.bean.sortBy=val;
		this.doQuery();
	},
	scrollQry:function(){
		if(this.canPage){
			this.bean.page++;
			this.clean=false;
			this.bean.qryTotal=null;
			this.canPage=false;
			this.doQuery();
		}else{
			//不处理 滚动分页需注意同步问题
		}
	},
	fillTable:function(data){
		if(data.total||data.total==0){
			$("#file_sort").find(".total").text("共"+data.total+"条记录");
		}
		if(data.rows&&data.rows.length){
			this.canPage=true;
		}else{
			this.canPage=false;
		}
		this.table.listTable({
			clean:this.clean,
			data:data.rows
		});
	},
	view:function(){
		var data=tableHandle.exec({
			table:this.table,
			func:'getId',
			data:$src()
		});
		$.java({
			action:'file.view',
			param:{id:data},
			callback:function(){
			
			}
		});
	},
	download:function(){
		var data=tableHandle.exec({
			table:this.table,
			func:'getId',
			data:$src()
		});
		$.java({
			action:'file.download',
			param:{id:data},
			callback:function(ret){
				location.href=ret.mes;
			}
		});
	}	
});


var common;
var jxd_data;
function setCommon(data){
	common=data;
};
//获取登录信息
yanhx.addCommon("ready",function(){
	$.java({
		action:'common.getLoginData',
		param:{str_array:[]},
		callback:function(data){
			for(var k in data){
				var val=data[k];
				yanhx.exec("mutilHandle.save",{
					key:k,
					val:val
				});
			}
		}
	});
});
function setData(data){
	jxd_data=data;
	for(var k in data){
		var val=data[k];
		$("#"+k).fillSelect(val);
		yanhx.exec("mutilHandle.save",{
			key:k,
			val:val
		});
	}
}
//获取文本及和数据字典
yanhx.addCommon("load",function(){
	var textFile=['file','comment'];
	$(textFile).each(function(){
		var t=this;
		$("<script src='/valid/"+t+"_valid.js' type='text/javascript' />").appendTo(document.head);
	});
	$.java({
		action:'common.text',
		param:{str_array:textFile},
		callback:setCommon
	});
	$.java({
		action:'common.data',
		param:{str_array:[]},
		callback:setData
	});
});
var _valid_={"2": {"noNull": null, "errorMsg": "", "valid": "", "id": "2", "name": "\u52a0\u8f7d\u4e8b\u4ef6"}, "file": {"noNull": null, "errorMsg": "", "valid": "", "id": "file", "name": "\u4e0a\u4f20\u6587\u4ef6"}};